package com.example.iot.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;

import com.example.iot.DB.DatabaseHelper;
import com.example.iot.R;
import com.example.iot.utils.OkHttpUtils;
import com.example.iot.utils.ToastUtils;

import java.io.IOException;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Response;

public class InitatiaActivity extends AppCompatActivity {
@BindView(R.id.bt_init_islogin)
    Button mybt_login;
OkHttpClient okHttpClient=new OkHttpClient();
ToastUtils toastUtils=new ToastUtils(this);
    DatabaseHelper dbHelper = new DatabaseHelper(InitatiaActivity.this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_initatia);
        ButterKnife.bind(this);
        //设置状态栏为透明
        Window window = getWindow();
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        window.setStatusBarColor(Color.TRANSPARENT);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        @SuppressLint("Recycle") Cursor cursor = db.rawQuery("select * from id",null);
        if(cursor.moveToNext()) {
            db.close();
        }else{
            OkHttpUtils.getSync("http://42.81.143.229:8091/getmkey", new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            toastUtils.showToastCenterLong("网络连接失败,请检查网络后重试。");
                        }
                    });

                }

                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                    if(response.isSuccessful()) {
                        final String id= Objects.requireNonNull(response.body()).string();
                        Log.e("id", String.valueOf(id));
                            ContentValues values = new ContentValues();
                            values.put("id",id);
                            db.insert("id", null, values);
                            db.close();

                    }else{
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                toastUtils.showToastTopLong("获取失败");
                            }
                        });

                    }

                }
            },okHttpClient);
        }

    }
    @OnClick(R.id.bt_init_islogin)
    public void islogin(){
        Intent i=new Intent(this, MainActivity.class);
        startActivity(i);
    }
}